package com.snow.useviewdemo.function.calendar;


import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-24
 * desc   :
 */
public class CalendarUtils {
    private static CalendarUtils calendarUtils;
    private static HashMap<String, List<CalendarData>> calendarMap = new HashMap<>();

    public static CalendarUtils init() {
        if (calendarUtils == null) {
            synchronized (CalendarUtils.class) {
                if (calendarUtils == null) {
                    calendarUtils = new CalendarUtils();
                }
            }
        }
        return calendarUtils;
    }

    /**
     * 填充日历数据
     *
     * @param calendarKey 2019-05 2019-12
     * @param calendarDataList
     */
    public void putCalendarDataList(String calendarKey, List<CalendarData> calendarDataList) {
        calendarMap.put(calendarKey, calendarDataList);
    }

    /**
     * 根据年月 2019-05 获取数据
     *
     * @param calendarKey 2019-05 2019-12
     * @return
     */
    public List<CalendarData> getCalendarDataList(String calendarKey) {
        List<CalendarData> calendarData = calendarMap.get(calendarKey);
        return calendarData;
    }

    /**
     * 在数字前添加0 比如：09 10
     *
     * @return
     */
    public String addZeroBefore(int num) {
        if (num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    /**
     * 获取对应年月的天数
     *
     * @param year
     * @param month
     * @return
     */
    public int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        return a.get(Calendar.DATE);
    }

    /**
     * 获取今天在month中的position
     *
     * @return
     */
    public int getTodayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        return currDay;
    }

    /**
     * 获取当前月份
     *
     * @return 8、11
     */
    public int getTodayMonth() {
        Calendar calendar = Calendar.getInstance();
        int currMouth = calendar.get(Calendar.MONTH) + 1;
        return currMouth;
    }

    /**
     * 获取当前年份
     *
     * @return 2019
     */
    public int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        return currYear;
    }

    /**
     * 对应年月的一号是星期几
     *
     * @param year
     * @param month
     * @return
     */
    public int getFirstDayOfWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 0);
        int currYear = calendar.get(Calendar.YEAR);
        int currMouth = calendar.get(Calendar.MONTH) + 1;
        int currDay = calendar.get(Calendar.DAY_OF_MONTH);
        int i1 = calendar.get(Calendar.DAY_OF_WEEK);//这就是星期几
        if (i1 == 7) {
            i1 = 0;
        }
        return i1;
    }

    /**
     * 调此方法输入所要转换的时间输入例如（"2014-06-14"）返回时间戳
     *
     * @param time
     * @return
     */
    public long dataToTime(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
                Locale.CHINA);
        Date date;
        long longTime = 0;
        try {
            date = sdr.parse(time);
            longTime = date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return longTime;
    }

    /**
     * 调此方法输入所要转换的时间输入例如（"2014-06-14"）返回dayOfMonth 14
     *
     * @param time
     * @return
     */
    public int dayOfMonth(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
                Locale.CHINA);
        Date date;
        int mydate = 0;
        try {
            date = sdr.parse(time);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mydate;
    }

    /**
     * 日历数据添加打卡日期
     *
     * @param beforeIndex      本月起始位置
     * @param calendarDataList 一整页显示的日历数据
     * @param clockInBeans     打卡数据
     */
    public void addClockInInfoToCalendarInfo(int year, int month, int beforeIndex, List<CalendarData> calendarDataList, List<ClockInBean> clockInBeans) {
        if (clockInBeans == null || clockInBeans.size() == 0) {
            return;
        }
        for (int i = 0; i < clockInBeans.size(); i++) {
            ClockInBean clockInBean = clockInBeans.get(i);
            String date = clockInBean.getDate();
            int dayOfMonth = dayOfMonth(clockInBean.getDate());
            if (beforeIndex + dayOfMonth <= calendarDataList.size() && !TextUtils.isEmpty(date) && date.contains(year + "-" + month)) {
                calendarDataList.get(beforeIndex + dayOfMonth - 1).setClockInBean(clockInBean);
            }
        }
    }
}
